package com.kingyun.gpsinspection.purificationservice.services.som.innerservice.impl;

import com.alibaba.fastjson.JSONObject;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.som.FacadeDeviceInfoMapper;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.som.FacadeDeviceOperateInfoMapper;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.som.FacadeEntityPropertyMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.som.FacadeDeviceInfo;
import com.kingyun.gpsinspection.purificationservice.facade.model.som.FacadeDeviceOperateInfo;
import com.kingyun.gpsinspection.purificationservice.facade.service.som.FacadeWorkCellInstanceService;
import com.kingyun.gpsinspection.purificationservice.facade.service.som.SomDeviceInfoService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 设备信息service实现类
 * Created by liuyj on 2017/1/13.
 */
public class SomDeviceInfoServiceImpl implements SomDeviceInfoService {
    @Autowired
    private FacadeDeviceInfoMapper facadeDeviceInfoMapper;
    @Autowired
    private FacadeEntityPropertyMapper facadeEntityPropertyMapper;
    @Autowired
    private FacadeDeviceOperateInfoMapper facadeDeviceOperateInfoMapper;
    @Autowired
    private FacadeWorkCellInstanceService facadeWorkCellInstanceService;

    /**
     * 查询所有设备
     *
     * @param userId
     * @return
     */
    @Override
    public List<FacadeDeviceInfo> findAllDeviceInfo(String userId) {
        List<FacadeDeviceInfo> list = facadeDeviceInfoMapper.queryByUserId(userId);
        List<FacadeDeviceInfo> newList = new ArrayList<>();
        if (list != null && list.size() > 0) {
            for (FacadeDeviceInfo deviceInfo : list) {
                // 设备故障信息
                deviceInfo.setFaults(facadeDeviceOperateInfoMapper.getByDeviceId(deviceInfo.getDeviceId()));
                // 设备操作记录
                deviceInfo.setResultBeen(findOperationRecordByDeviceId(deviceInfo.getDeviceId()));
                // 设备特性值
                deviceInfo.setProperties(facadeEntityPropertyMapper.queryByEntityId(deviceInfo.getEntityId()));
                // 设备工作标准
                deviceInfo.setCells(facadeWorkCellInstanceService.queryByEntityId(deviceInfo.getEntityId()));
                newList.add(deviceInfo);
            }
        }
        return newList;
    }

    /**
     * 根据设备ID查询操作记录
     *
     * @param deviceId
     * @return
     */
    @Override
    public List<FacadeDeviceOperateInfo> findOperationRecordByDeviceId(String deviceId) {
        return facadeDeviceOperateInfoMapper.queryByDeviceId(deviceId);
    }

    @Override
    public JSONObject getDeviceMean() {
        JSONObject deviceInfo = new JSONObject();
        deviceInfo.put("deviceId", "设备id");
        deviceInfo.put("deviceInnerCode", "设备内部编码");
        deviceInfo.put("deviceCode", "企业设备自编码");
        deviceInfo.put("deviceName", "设备名");
        deviceInfo.put("orgId", "组织id");
        deviceInfo.put("parentDeviceId", "上级设备ID");
        deviceInfo.put("parentDeviceName", "上级设备名称");
        deviceInfo.put("instoreTime", "设备入库时间");
        deviceInfo.put("outstoreTime", "设备出库时间");
        deviceInfo.put("inuseTime", "设备启用时间");
        deviceInfo.put("status", "设备状态");
        deviceInfo.put("devicePic1", "设备图片1");
        deviceInfo.put("devicePic2", "设备图片2");
        deviceInfo.put("deviceModel", "设备型号");
        deviceInfo.put("energyConsumption", "额定能耗");
        deviceInfo.put("totalPower", "总功率");
        deviceInfo.put("weight", "重量");
        deviceInfo.put("unitOfWeight", "重量单位");
        deviceInfo.put("ageLimit", "效用年限");
        deviceInfo.put("buyDate", "购置日期");
        deviceInfo.put("madeDate", "出厂年月");
        deviceInfo.put("sizeDimension", "大小/尺寸");
        deviceInfo.put("stationNumber", "站内编号");
        deviceInfo.put("technicalIdentificationNum", "技术标识号");
        deviceInfo.put("rfid", "rfid");
        deviceInfo.put("factory", "生产安装单位");
        deviceInfo.put("stationPlace", "安装位置");
        return deviceInfo;
    }

    /**
     * 更新rfid
     *
     * @param entityId
     * @return
     */
    @Override
    public int updateRfid(String entityId, String rfid) {
        Map map = new HashMap<>();
        map.put("entityId", entityId);
        map.put("rfid", rfid);
        return facadeDeviceInfoMapper.updateRfid(map);
    }
}
